﻿Imports Global_Visas_Business_Layer
Imports System.IO
Imports System.Data

Partial Public Class frmDocumentList
    Private _DocStoreID As Integer

    Public Property DocStoreID() As Integer
        Get
            Return _DocStoreID
        End Get
        Set(ByVal value As Integer)
            _DocStoreID = value
        End Set
    End Property

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmDocumentList_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Escape Then
            Me.Close()
        End If

    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPrint.Click
        Try

        
            If Me.lvwDocuments.SelectedItems.Count = 0 Then
                Call CommonFuncs.DisplayMessage("No documents selected!", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If

            Dim lvi As DataRowView

            For Each lvi In Me.lvwDocuments.SelectedItems
                Dim s As String = lvi.Row.ItemArray(2).ToString
                If Not s = String.Empty Then
                    Documents.PrintFile(s)
                End If

            Next
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmDocumentList.btnPrint_Click")
        End Try
    End Sub

    Private Sub frmDocumentList_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Try
            GeneralTasks.ChangeTheme(Me)
            GeneralTasks.EnumerateandFill(Me.LogicalChildren)
            DataBinding.BindListView(Documents.GetDocsInDocStore(Me.DocStoreID), Me.lvwDocuments)
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmDocumentList.frmDocumentList_Loaded")
        End Try
    End Sub

    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCopy.Click, btnCopyOpen.Click

        Dim lvi As DataRowView
        Dim strPath As String
        Dim strFileFromName As String
        Dim strFileToName As String
        ' Dim DbServerName, DbServerName1, newfile As String
        Try
            Dim configurationAppSettings As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader
            If Me.lvwDocuments.SelectedItems.Count = 0 Then
                Call CommonFuncs.DisplayMessage("No documents selected!", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If

            If Me.DocStoreID > 0 Then
                strPath = "H:" ' & Me.DocStoreID
            Else
                CommonFuncs.DisplayMessage("The DocStoreID could not be found. Please contact IT.", CommonDialog.CustomDialogIcons.Information, "DocStoreID")
                Exit Sub
            End If

            If Not Directory.Exists(strPath) Then
                Directory.CreateDirectory(strPath)
            End If

            ' DbServerName = CommonDataFunctions.GetCustomizationValue("VirtualDocDir")
            ' DbServerName1 = CType(configurationAppSettings.GetValue("DocStoreServer", GetType(System.String)), String)
            'newfile = DbServerName1.Replace("\\" & DbServerName1 & "\", DbServerName)
            Dim splitPath() As String
            For Each lvi In Me.lvwDocuments.SelectedItems

                strFileFromName = lvi.Row.ItemArray(2).ToString
                'splitPath = Split(strFileFromName, "\")
                'newfile = strFileFromName.Replace("\\" & DbServerName1 & "\DocStore$\", DbServerName)
                'strFileToName = strPath & "\" & splitPath(4) & "\" & splitPath(5) & "\" & DocStoreID & "\" & IO.Path.GetFileName(lvi.Row.ItemArray(2).ToString)

                If Not File.Exists(strFileFromName) Then
                    CommonFuncs.DisplayMessage("Could not find the file: " & strFileFromName, CommonDialog.CustomDialogIcons.Stop, "Error")
                End If
                'If Letters.DownloadFileWebservice(FileServer.VirtualDir.Files, splitPath(4) & "\" & splitPath(5) & "\", DocStoreID, IO.Path.GetFileName(lvi.Row.ItemArray(2).ToString), strPath) = True Then
                Dim btn As Button = CType(sender, Button)
                If btn.Name.ToString = "btnCopyOpen" Then
                    If Documents.OpenFile(strFileFromName) = False Then
                        CommonFuncs.DisplayMessage("Could not open the file: " & strFileFromName, CommonDialog.CustomDialogIcons.Stop, "Error")
                    End If
                    'ElseIf btn.Name.ToString = "btnCopy" Then
                    '    CommonFuncs.DisplayMessage("The file has been saved in the path : " & strPath & "\" & splitPath(4) & "\" & splitPath(5) & "\" & DocStoreID & "\" & IO.Path.GetFileName(lvi.Row.ItemArray(2).ToString), CommonDialog.CustomDialogIcons.Information, "Info")
                End If
                'End If
                'download the file instead of copy
                'If Letters.DownloadFileFromHttpOrFtp(newfile, strFileToName, True, GlobalCRM.CurrentNTUserName, GlobalCRM.CurrentNTPassword) = "True" Then



                '    'File.Copy(strFileFromName, strFileToName)

                '    Dim btn As Button = CType(sender, Button)
                '    If btn.Name.ToString = "btnCopyOpen" Then
                '        If Documents.OpenFile(strFileToName) = False Then
                '            CommonFuncs.DisplayMessage("Could not open the file: " & strFileToName, CommonDialog.CustomDialogIcons.Information, "Error")
                '        End If
                '    End If
                'End If
            Next
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmDocumentList.btnCopy_Click")
        End Try
    End Sub



    Private Sub btnEmail_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEmail.Click
        Try

        
            Dim lvi As DataRowView

            Dim frmSendEmail As New frmSendEmail


            For Each lvi In Me.lvwDocuments.SelectedItems
                frmSendEmail.lstAttach.Items.Add(lvi.Row.ItemArray(2).ToString)
                GlobalCRM.LocalAttachments.AddAttachment(IO.Path.GetFileName(lvi.Row.ItemArray(2).ToString), lvi.Row.ItemArray(2).ToString, 1)
            Next


            frmSendEmail.ShowDialog()

            Me.Close()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmDocumentList.btnEmail_Click")
        End Try
    End Sub

    Private Sub btnPrintAll_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPrintAll.Click
        Try


            If Me.lvwDocuments.Items.Count = 0 Then
                Call CommonFuncs.DisplayMessage("No documents Available", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If

            Dim lvi As DataRowView
            If CommonFuncs.DisplayyesNoMessage("Are you sure to print all the documents?", CommonDialog.CustomDialogIcons.Question, "Print") = 1 Then


                For Each lvi In Me.lvwDocuments.Items
                    Dim s As String = lvi.Row.ItemArray(2).ToString
                    If Not s = String.Empty Then
                        Documents.PrintFile(s)
                    End If

                Next
            Else
                Exit Sub
            End If
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmDocumentList.btnPrint_Click")
        End Try
    End Sub
End Class
